class OperationsController < ApplicationController
  layout "popup", :except => :create_remote
  protect_from_forgery :only => [:create, :destroy, :update]

  def index
    @operations = Operation.find(:all)

    respond_to do |format|
      format.html
      format.xml  { render :xml => @operations }
    end
  end

  def new
    @operation = Operation.new

    respond_to do |format|
      format.html
      format.xml  { render :xml => @operation }
    end
  end

  def edit
    @operation = Operation.find(params[:id])
  end

  def create
    @operation = Operation.new(params[:operation])

    respond_to do |format|
      if @operation.save
        flash[:notice] = 'Operazione creata con successo.'
        format.html { redirect_to(operations_url) }
        format.xml  { render :xml => @operation, :status => :created, :location => @operation }
        format.js { render :action => "create.rjs" }
      else
        flash[:notice] = 'Operazione non creata!'
        format.html { render :action => "new" }
        format.xml  { render :xml => @operation.errors, :status => :unprocessable_entity }
        format.js { render :action => "shared/error.rjs" }
      end
    end
  end

  def update
    @operation = Operation.find(params[:id])

    respond_to do |format|
      if @operation.update_attributes(params[:operation])
        flash[:notice] = 'Operazione aggiornata con successo.'
        format.html { redirect_to(operations_url) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @operation.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @operation = Operation.find(params[:id])
    @operation.destroy

    respond_to do |format|
      format.html { redirect_to(operations_url) }
      format.xml  { head :ok }
    end
  end
end
